const IS_TEST = true; //是否测试 打包时一定要注意值必须为false;
const NAME_SPACE = '_fufu_'; //命名空间
const SAND_BOX_API = 'http://localhost:8081'; // 沙箱地址
const OFFICIAL_API = 'https://www.sundao.live/'; // 正式环境地址
const ROUTE_TYPE = 'history'; //ROUTER模式 hash还是history
const API_ROOT = IS_TEST ? SAND_BOX_API : OFFICIAL_API;
const LOGIN_ROUTE = '/pages/public/login'; //登录地址
const REGISTER_ROUTE = ''; //注册地址 /pages/public/reg
const UPDATE_API = API_ROOT + 'upgrade';
const ENCRYPT = false; //是否开启数据加密
const EFFECT_ON = false; //是否开启页面效果
const MAIN_COLOR = 'rgba(128,128,0,1)';
import Msg from './message.js';
/**
 * 登录相关
 */
const login = (token,address) => {
	uni.setStorageSync(NAME_SPACE + 'token_', token);
	uni.setStorageSync(NAME_SPACE + 'address_', address);
}
const logout = () => {
	uni.removeStorageSync(NAME_SPACE + 'token_');
	uni.removeStorageSync(NAME_SPACE + 'address_');
}
const token = () => {
	return uni.getStorageSync(NAME_SPACE + 'token_') || '';
}
const isLogin = () => {
	return uni.getStorageSync(NAME_SPACE + 'token_') || false;
}
const getAddress = () => {
	return uni.getStorageSync(NAME_SPACE + 'address_') || false;
}
const toLogin = () => {
	Msg.Fail('请先登录再操作!', () => {
		uni.redirectTo({
			url: LOGIN_ROUTE
		})
	})
}

/**
 * 分享相关
 */

const setParentIdByU = (u) => {
	if (u) uni.setStorageSync(NAME_SPACE + 'fid_', u);
}
const getParentId = () => {
	return uni.getStorageSync(NAME_SPACE + 'fid_') || '';
}
const setParentId = (e) => {
	if (e.u) uni.setStorageSync(NAME_SPACE + 'fid_', e.u);
}

const getWapShareUrl = (isIndex, uid) => {
	let surl = !isIndex ? REGISTER_ROUTE : '/';
	if (ROUTE_TYPE == 'hash') surl = '/#' + surl;
	// #ifdef H5
	let protocol = location.protocol;
	let hostname = location.hostname;
	let port = location.port;
	let _port = parseInt(port) != 80 && parseInt(port) != 443 && port != '' && port != null;
	let p = _port ? ':' + port : '';
	let url = protocol + '//' + hostname + p + surl + '?u=' + uid
	return url;
	// #endif
	let b = API_ROOT.split('/');
	return b[0] + '//' + b[2] + surl + '?u=' + uid;
}


/**
 * 其他配置
 */
const settig = {
	'root': API_ROOT,
	'encrypt': ENCRYPT,
	'login_route': LOGIN_ROUTE,
	'update_api': UPDATE_API,
	'effect_on': EFFECT_ON,
	'main_color': MAIN_COLOR
};
const getAttr = (name, e) => {
	return e.currentTarget.dataset[name];
}
const meTitle = (title) => {
	uni.setNavigationBarTitle({
		title: title
	})
}
const loading = (msg) => {
	if (!msg) msg = '加载中'
	uni.showLoading({
		title: msg
	})
}

module.exports = {
	cfg: settig,
	getAttr,
	loading,
	meTitle,
	getWapShareUrl,
	token,
	isLogin,
	login,
	logout,
	toLogin,
	getParentId,
	setParentId,
	setParentIdByU,
	getAddress
}
